<template>
  <div class="views-container">
    <div class="wlm-table">
      <div class="wlm-table-hearder-btn">
        <div class="wlm-table-hearder-btn " style="display:flex;justify-content:space-between;">
           <router-link class="wlm-text" tag="span" to="/application/themeColorAdd">
             <el-button size="small" type="primary">新建主题</el-button>
           </router-link>
           <div>
          是否需要开启多渠道：
          <el-radio-group v-model="open" @change="changeOpen">
            <el-radio label="1">是</el-radio>
            <el-radio label="2">否</el-radio>
          </el-radio-group>
        </div>
        </div>

      </div>
      <div class="wlm-table-content">
        <el-table :ref="tableFormatData.colorTable.key" :data="tableFormatData.colorTable.tableData" style="width: 100%">
          <el-table-column prop="name" label="ID">
            <template slot-scope="scope">
              <span>{{scope.row.theme_id}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="title" label="主题名称">
            <template slot-scope="scope">
              <img v-if="scope.row.source && scope.row.is_set_theme==1 && open==1" height="20" width="20" :src="sourceImg[`${scope.row.source || '-'}`]" alt />
              <span>{{scope.row.theme_name}}</span>
              <el-tag style="width: 48px;font-size:12px;" size="mini" v-if="scope.row.is_set_theme==1 ? scope.row.is_set_theme : ''" >使用中</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="date" label="创建日期">
            <template slot-scope="scope">
              <span>{{scope.row.create_time}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="date" label="修改日期">
            <template slot-scope="scope">
              <span>{{scope.row.update_time}}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <div class="operation-group">
                <router-link class="wlm-text" tag="span" :to="{path:'/application/themeColorAdd',query:{theme_id:scope.row.theme_id,color_scheme:scope.row.color_scheme} }">
                  <el-button type="text">编辑</el-button>
                </router-link>

                  <div class="btn-line"></div>
                <el-button class="wlm-text" type="text" @click="delTecolor(scope.row.theme_id)">删除</el-button>

                  <div class="btn-line"></div>
                  <template  v-if="scope.row.is_set_theme==2">
                    <el-button
                    class="wlm-text"
                    type="text"
                   @click="toggle({theme_id: scope.row.theme_id,is_set_theme:1},tableFormatData.colorTable.change.colorBtn)"
                  >设为主题</el-button>
                  </template>
                  <template  v-if="scope.row.is_set_theme==1">
                    <el-button
                    class="wlm-text"
                    type="text"
                   @click="toggle({theme_id: scope.row.theme_id,is_set_theme:2},tableFormatData.colorTable.change.colorBtn)"
                  >取消主题</el-button>
                  </template>
                  <template v-if="scope.row.is_set_theme==1 && open==1">
                  <div class="btn-line"></div>
                  <el-button class="wlm-text" type="text" @click="toggle({theme_id: scope.row.theme_id,is_set_theme:1},tableFormatData.colorTable.change.colorBtn)">
                    更换渠道</el-button>
                </template>
              </div>
            </template>
          </el-table-column>
        </el-table>
<!-- 分页 -->
        <div class="pagination-content flex-row flex-justify-b flex-align-c">
          <el-row class="pagination-btns">
            <!-- <el-checkbox class="check-all" true-label="1" false-label="0"  @change="toggleSelection">全部</el-checkbox>
            <el-button class="right-8" :disabled="isGroup" size="mini" @click="delTableList">删除</el-button> -->
          </el-row>
          <el-pagination
              :disabled="!hasTableData"
              @size-change="listPageChange"
              @current-change="listPageChange"
           :current-page.sync="tableFormatData.colorTable.pagination.page"
            :page-sizes="tableFormatData.colorTable.pagination.pagesizes"
            :page-size.sync="tableFormatData.colorTable.pagination.list_rows"
            layout="total, sizes, prev, pager, next, jumper"
              :total="hasTableData?tableFormatData.colorTable.pagination.total : 0">
          </el-pagination>
        </div>
      </div>
    </div>

    <el-dialog :visible.sync="dialogVisibleSource" width="30%" custom-class="reset-pwd wlm-form-dialog dialog-table">
      <div class="wlm-form" v-if="dialogVisibleSource">
        <div class="wlm-form-header wlm-dialog-fixheader">
          <span class="wlm-dialog-inlenr">选择渠道</span>
        </div>
        <el-scrollbar wrap-class="scrollbar-wrapper">
          <div class="wlm-form-content">
            <div class="wlm-dialog-main">
              <div class="wlm-dialog-content">
                <el-form ref="userForm" :model="formData" style="width:200px;" label-width="120px" class="retail-form" label-position="right">
                  <el-form-item label="选择渠道">
                    <el-radio-group v-model="formData.source">
                      <el-radio label="2" v-if="JSON.stringify($store.getters.power).indexOf('wxapp')>-1">微信小程序</el-radio>
                      <el-radio label="3" v-if="JSON.stringify($store.getters.power).indexOf('H5')>-1">H5</el-radio>
                      <el-radio label="4" v-if="JSON.stringify($store.getters.power).indexOf('wechat')>-1">微信公众号</el-radio>
                      <el-radio label="5" v-if="JSON.stringify($store.getters.power).indexOf('android')>-1">安卓</el-radio>
                      <el-radio label="6" v-if="JSON.stringify($store.getters.power).indexOf('ios')>-1">苹果</el-radio>
                      <el-radio label="7" v-if="JSON.stringify($store.getters.power).indexOf('aliapp')>-1">支付宝小程序</el-radio>
                      <el-radio label="9" v-if="JSON.stringify($store.getters.power).indexOf('toutiaoapp')>-1">抖音-头条</el-radio>
                      <el-radio label="8" v-if="JSON.stringify($store.getters.power).indexOf('baiduapp')>-1">百度小程序</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </el-form>
              </div>
            </div>
          </div>
        </el-scrollbar>
      </div>
      <footer class="flex-row flex-align-c flex-justify-c wlm-dialog-fixfooter">
        <el-button size="small" type="primary" @click="save">确定</el-button>
      </footer>
    </el-dialog>
  </div>
</template>
<script>
import {
  getDiyThemeList,
  delDiyThemeOpen,
  setDiyThemeOpen,
  sourceOpenSave,
  applicationSourceOpen
} from '@/api/platform'
import mixins from '@/mixins/mixins'
export default {
  mixins: [mixins.getters('Table')],
  name: 'ColorTable',
  data() {
    return {
      open: '1',
      params: { theme_id: '', type: '', source: [] },
      dialogVisibleSource: false,
      formData: {
        source: ''
      },
      tableFormatData: {
        current: 'colorTable',
        colorTable: {
          key: 'colorTable',
          api: {
            getList: getDiyThemeList,
            delList: delDiyThemeOpen
          },
          tableData: [],
          files: {
            theme_id: ''
          },
          change: {
            colorBtn: {
              key: 'is_set_theme',
              api: setDiyThemeOpen
            }
          },
          // 表格分页
          pagination: {
            page: 1,
            list_rows: 10,
            pagesizes: [10, 20, 50],
            total: 0
          }
        }
      }
    }
  },
  mounted() {
    this.getIsOpen()
  },
  methods: {
    getIsOpen() {
      applicationSourceOpen().then(res => {
        this.open = res.data.data.is_more_source
      })
    },
    changeOpen() {
      sourceOpenSave({ is_more_source: this.open }).then(res => {
        const { code } = res.data
        code == 1 && this.$message.success('操作成功')
      })
    },

    toggle({ theme_id, is_set_theme }) {
      if (this.open == 1) {
        this.dialogVisibleSource = true
        this.params.source = ''
        this.params = { theme_id, is_set_theme, source: '' }
      } else {
        this.changeTableItem({ theme_id, is_set_theme }, this.tableFormatData.colorTable.change.colorBtn)
      }
    },
    save() {
      this.params.source = this.formData.source
      if (!this.params.source) {
        this.$message.warning('请选择渠道')
        return false
      }
      this.changeTableItem(this.params, this.tableFormatData.colorTable.change.colorBtn)
      this.dialogVisibleSource = false
    },

    // 删除
    delTecolor(theme_id) {
      this.tableFormatData.colorTable.files.theme_id = theme_id
      this.delTableItem(theme_id)
    }
  },
  computed: {}
}
</script>
<style lang="less" scoped>
</style>
